<?php
namespace Admin\Logic;

use Think\Log;

class TableLogic
{

    /**
     * 获取餐桌列表
     * @param array $where
     * @return mixed
     */
    public static function getAll()
    {
        try {
            $table = D('table as t');
            $data = $table
                ->join('left join food_member as m on m.id=t.member_id')
                ->field('t.*,m.nickname')
                ->order('id desc')
                ->select();
            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 执行添加评论操作
     * @param $param
     * @return array|bool
     */
    public static function create($param)
    {
        try {
            $user = M('table');

            $id = $user->add($param);

            if($id){
                return ['code' => 0, 'msg' => $id];
            } else {
                return ['code' => 1, 'msg' => '服务器找不到了'];
            }

        } catch (\Exception $e) {

            return false;
        }
    }

    /**
     * 判断餐桌名子是否已经存在
     * @param $name
     * @param int $id
     * @return array|bool
     */
    public static function tableNameIsExist($name,$id = 0)
    {

        try {
           if($id == 0){
               $res = D('table')->where(['table_name'=>$name])->select();
           } else {
               $res = D('table')->where('id!='.$id)->where(['table_name'=>$name])->select();
           }


           if(empty($res)){
               return ['code'=>0];
           } else {
               return ['code'=>1,'msg'=>'已存在的餐桌名'];
           }

        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 执行添加评论操作
     * @param $param
     * @return array|bool
     */
    public static function createMessage($param)
    {
        try {
            $user = M('message');

            $id = $user->add($param);

            if($id){
                return ['code' => 0, 'msg' => $id];
            } else {
                return ['code' => 1, 'msg' => '服务器找不到了'];
            }

        } catch (\Exception $e) {

            return false;
        }
    }

    /**
     * 删除单条数据
     * @param $id
     * @return mixed
     */
    public static function delete($id)
    {
        try {
            $data = D('table')->where(['id'=>$id])->delete();
            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    public static function editMember($arr,$member_id)
    {
        $table = D('table');
        try{
            $result = $table->where(['id'=>['in',$arr]])->save(['member_id'=>$member_id]);

            if($result){
                return ['code' => 1];
            } else {
                return ['code' => 1, 'msg' => '添加负责餐桌失败！请重试'];
            }

        } catch (\Exception $e) {
            return false;
        }
    }


}